--[[
  江西银行 AFANATP - 9002端口
]]
require("apm_core")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt, ...) end
    function logInfo(fmt, ...) end
    function logWarn(fmt, ...) end
    function logError(fmt, ...) end
    function logFatal(fmt, ...) end
end

local KRED = '\x1B[0;31m'
local KSALMON = '\x1B[1;31m'
local KGREEN = '\x1B[0;32m'
local KLIMEGREEN = '\x1B[1;32m'
local KYELLOW = '\x1B[0;33m'
local KORANGE = '\x1B[0;34m'
local KBLUE = '\x1B[1;34m'
local KMAGENTA = '\x1B[0;35m'
local KPINK = '\x1B[1;35m'
local KCYAN = '\x1B[0;36m'
local RESET = '\x1B[0m'

local dict_transname = {
    ["EW0030"] = "发起自由格式书",
    ["EW0031"] = "收到自由格式书",
    ["EW0032"] = "发起业务撤销",
    ["EW0033"] = "发起跨行账号信息查询",
    ["EW0034"] = "网银交易状态下载",
    ["EW0231"] = "接收业务状态查询应答",
    ["EW0232"] = "接收撤销应答",
    ["EW0233"] = "接收账户信息查询应答",
    ["EW0234"] = "收到账户信息查询",
    ["EW0235"] = "收到账户明细查询",
    ["IBPS0001"] = "超级网银单笔记账",
    ["IBPS0002"] = "超级网银账务通讯",
    ["IBPS0003"] = "发起通讯级报文",
    ["IBPS0004"] = "账户详细信息查询",
    ["IBPS0005"] = "核心交易状态同步",
    ["IBPS0006"] = "账户信息查询",
    ["IBPS0007"] = "来账贷记入账检查",
    ["EW0040"] = "发起授权支付协议签约申请",
    ["EW0042"] = "发起账号信息查询协议签约申请",
    ["EW0043"] = "发起协议解约",
    ["EW0046"] = "接收网银URL身份验证应答",
    ["EW0062"] = "接收授权支付协议身份认证成功通知",
    ["EW0063"] = "接收账户信息查询协议身份认证成功通知",
    ["EW0244"] = "接收授权支付协议管理申请",
    ["EW0245"] = "接收账户信息查询协议管理申请",
    ["EW0246"] = "接收授权支付协议管理通知",
    ["EW0247"] = "接收账户信息查询协议管理通知",
    ["EW0248"] = "接收授权支付协议管理申请应答",
    ["EW0249"] = "接收账户信息查询协议管理申请应答",
    ["EW0250"] = "接收授权支付协议管理通知应答",
    ["EW0251"] = "接收账户信息查询协议管理通知应答",
    ["EW0281"] = "行别变更通知",
    ["EW0282"] = "参与机构变更通知",
    ["EW0283"] = "系统参数变更通知",
    ["EW0284"] = "支付业务金额上限变更通知",
    ["EW0285"] = "计费与返还清单",
    ["EW0001"] = "往账贷记",
    ["EW0002"] = "往账借记",
    ["EW0003"] = "第三方贷记(作为付款行发起)",
    ["EW0005"] = "第三方贷记(只作为受理行发起)",
    ["EW0006"] = "第三方贷记往帐",
    ["EW0007"] = "授权第三方贷记(作为收款行发起)",
    ["EW0009"] = "第三方贷记往账(总)",
    ["EW0010"] = "第三方贷记第三方接收带网关URL",
    ["EW0011"] = "柜面往账贷记",
    ["EW0061"] = "第三方贷记验证URL通知",
    ["EW0101"] = "往账贷记第三方接入",
    ["afa_maintransdtl"] = "转移主表数据",
    ["EW0201"] = "来帐贷记接收流程",
    ["EW0202"] = "来账借记接收流程",
    ["EW0204"] = "接收第三方贷记URL回执",
    ["EW0210"] = "第三方贷记来帐总控",
    ["EW0211"] = "第三方贷记扣款子流程",
    ["EW0212"] = "第三方贷记入账子流程",
    ["EW0213"] = "第三方贷记获取URL子流程",
    ["EW0021"] = "根据渠道信息查询支付平台处理结果",
    ["EW0022"] = "查询业务手续费金额",
    ["EW0023"] = "查询参与机构信息",
    ["EW0024"] = "网银查询账户余额",
    ["EW0025"] = "网银查询账户明细",
    ["EW0027"] = "查询授权支付协议信息",
    ["EW0028"] = "查询跨行账户查询协议信息",
    ["EW0029"] = "查询协议操作流水",
    ["EW0035"] = "查询第三方明细信息",
    ["EW0036"] = "人行计费及返还清单查询",
    ["EW0037"] = "查询自由格式书",
    ["EW0038"] = "查询人行系统参数",
    ["EW0039"] = "查询支付业务金额上限",
    ["EW0054"] = "查询手续费信息",
    ["EW0055"] = "登记手续费信息",
    ["EW0056"] = "修改手续费信息",
    ["EW0057"] = "删除手续费信息",
    ["EW0090"] = "回单打印",
    ["EW0091"] = "系统参数维护",
    ["EW0096"] = "网银互联系统参数查询",
    ["EW0099"] = "支付系统行号查询",
    ["EW0100"] = "网银单笔明细查询",
    ["EW0102"] = "第三方单笔业务查询",
    ["EWE100"] = "查询第三方明细信息",
    ["EW0177"] = "往账贷记补扣客户账",
    ["EW0255"] = "对账总控",
    ["EW0270"] = "收到人行清算通知",
    ["EW0276"] = "收到人行汇总核对",
    ["EW0277"] = "收到人行业务明细核对应答",
    ["EW0278"] = "自动对账",
    ["EW0279"] = "申请人行明细异步调用",
    ["EW1100"] = "请求主机对账文件",
    ["EWC806"] = "往账贷记来账借记轧差失败重新轧差",
    ["EWC809"] = "清算记账失败重新记账",
    ["EWC810"] = "日终差错处理",
    ["EWC811"] = "日终差错处理异步调用",
    ["EWC999"] = "行内主机对账核对",
    ["EW0050"] = "发起系统登录/签退",
    ["EW0051"] = "系统状态变更通知",
    ["EW0052"] = "接收系统登录/签退异步回执",
    ["EW0058"] = "发起数字证书绑定通知",
    ["EW0252"] = "收到强制离线通知",
    ["EW0253"] = "收到停运通知",
    ["EW0254"] = "收到数字证书绑定通知",
    ["EW0286"] = "收到参与机构状态变更报文",
    ["EW0167"] = "接收账户信息查询人行回执处理",
    ["EW0168"] = "协议通知流程",
    ["EW0169"] = "调用小额转账接口",
    ["EW0170"] = "接收往贷人行回执报文处理",
    ["EW0171"] = "接收来账贷记人行回执处理",
    ["EW0172"] = "接收往借人行回执报文处理",
    ["EW0173"] = "接收来账借记人行回执处理",
    ["EW0174"] = "往贷往借来借自动冲账",
    ["EW0175"] = "入账超时失败后重新入账",
    ["EW0176"] = "柜面冲账",
    ["EW0265"] = "人行通用处理确认",
    ["EW0266"] = "接收通信级确认报文",
    ["EW0300"] = "轧差通知",
    ["EW0302"] = "轧差后通知特殊卡系统",
    ["EW0310"] = "报文丢弃通知",
    ["EW0311"] = "柜面日间异常处理",
    ["EW9001"] = "柜面超级网银贷记信息查询",
    ["EW9003"] = "超级网银来往账统计",
    ["EW9005"] = "保证金退转文件生成",
    ["EW9006"] = "日终签退检查",
}

local dict_channelname = {
    ["EUB"] = "VTM",
    ["ATM"] = "本行ATM",
    ["BCK"] = "后台",
    ["CAL"] = "电话人工",
    ["CDS"] = "自助取款机",
    ["CPR"] = "合作方",
    ["CUA"] = "中国银联ATM",
    ["CUP"] = "中国银联POS",
    ["DSK"] = "柜面",
    ["INT"] = "个人网上银行",
    ["PAY"] = "电子支付",
    ["POS"] = "本行POS",
    ["STM"] = "自助终端",
    ["TEL"] = "电话语音",
    ["MPH"] = "手机银行",
    ["007"] = "手机银行",
    ["012"] = "网上银行",
    ["013"] = "第三方业务",
}

function post_analysis()
    local ApmData = require("ApmData")

    local s_transcode
    local s_transname
    local s_channelcode
    local s_channelname
    local s_flowno
    local s_rspcode
    local s_rspmsg

    local s_srcip = ApmData.getSrcIp()
    if s_srcip == "10.1.1.117" or
            s_srcip == "10.1.1.118" or
            s_srcip == "10.1.1.93" or
            s_srcip == "10.1.1.94" then
        ApmData.replaceField("request.isResponseRequired", 0)
    end

    if ApmData.getField("request.jxbank_afanatp.signstr", 1) ~= nil then
    		ApmData.replaceField("request.jxbank_afanatp.signstr", "***")
    end
    
    ApmData.delFields('request.jxbank_afanatp."CNY"')

    -- 交易类型
    s_transcode = ApmData.getField("transaction.type_code", 1)
    if s_transcode ~= nil and s_transcode ~= "" then
        s_transname = dict_transname[s_transcode]
        if s_transname == nil then s_transname = s_transcode end
    end

    -- 渠道类型
    s_channelcode = ApmData.getField("request.jxbank_afanatp.channelcode", 1)
    if s_channelcode ~= nil and s_channelcode ~= "" then
        s_channelname = dict_channelname[s_channelcode]
        if s_channelname == nil then s_channelname = s_channelcode end
    end

    -- 返回码
    s_rspcode = ApmData.getField("response.jxbank_afanatp.errorcode", 1)
    s_rspmsg = ApmData.getField("response.jxbank_afanatp.errormsg", 1)
    if s_rspcode == nil or s_rspcode == "" then
        s_rspcode = ApmData.getField("response.jxbank_afanatp.dealcode", 1)
        s_rspmsg = ApmData.getField("response.jxbank_afanatp.dealmsg", 1)
    end

    if s_transcode == nil or s_transcode == "" then
        logInfo("s_transcode is nil [%s]", ApmData.dumpFields(true))
    else
        ApmData.replaceField("transaction.type_code", s_transcode)
        ApmData.replaceField("transaction.type_name", s_transname)
        ApmData.replaceField("transaction.title", s_transcode)

        if s_channelcode ~= nil and s_channelcode ~= "" then
            ApmData.replaceField("transaction.channel_code", s_channelcode)
            ApmData.replaceField("transaction.channel_name", s_channelname)
        end

        if s_rspcode ~= nil then
            ApmData.replaceField("response.code_string", s_rspcode)
            ApmData.delFields("response.code")
        end

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        logInfo("AFANATP 9002(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_transname,
            s_channelname,
            s_flowno,
            s_rspcode,
            s_rspmsg)
    end

    return
end

--AFANATP
--10.1.1.86:9002
--10.1.1.87:9002
--10.1.1.88:9002
--v1.0.6
